﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;

namespace DataAchive.Helper
{
    public class FileHelper
    {

        public void OpenFolder(string path)
        {
            // 检查是否已经有相同的进程在运行
            bool isAlreadyOpen = false;
            foreach (Process process in Process.GetProcesses())
            {
                if (process.MainWindowTitle.Contains(path))
                {
                    // 如果找到相同路径的进程，则激活窗口并退出方法
                    isAlreadyOpen = true;
                    NativeMethods.SetForegroundWindow(process.MainWindowHandle);
                    break;
                }
            }

            // 如果没有相同的进程在运行，则启动新的进程打开文件夹
            if (!isAlreadyOpen)
            {
                try
                {
                    Process.Start("explorer.exe", path);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("打开文件夹时出错：" + ex.Message);
                }
            }
        }

        // P/Invoke 导入 SetForegroundWindow 方法
        internal static class NativeMethods
        {
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            internal static extern bool SetForegroundWindow(IntPtr hWnd);
        }


        public static FileInfo GetFileInfo(string filePath)
        {
            // 创建 FileInfo 对象
            FileInfo fileInfo = new FileInfo(filePath);

            // 检查文件是否存在
            if (fileInfo.Exists)
            {
                return fileInfo;
            }
            else
            {
                Console.WriteLine("File does not exist.");
                return null;
            }
        }

        /// <summary>
        /// 检查文件路径是否包含相同文件名的文件，返回不重复文件名
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetFileName(string directoryPath, string fileName)
        {
            string filePath = Path.Combine(directoryPath, fileName); // 文件路径

            // 如果文件存在，则在文件名后面添加数字
            int suffix = 1;
            while (File.Exists(filePath))
            {
                string name = $"{Path.GetFileNameWithoutExtension(fileName)}({suffix}){Path.GetExtension(fileName)}";
                filePath = Path.Combine(directoryPath, name);
                suffix++;
            }
            return filePath;

        }

        /// <summary>
        /// 获取文件夹内所有文件
        /// </summary>
        /// <param name="folderPath"></param>
        public static string[] ListAllFiles(string folderPath)
        {
            try
            {
                // 获取文件夹内所有文件的完整路径
                string[] files = Directory.GetFiles(folderPath);

                return files;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
                return null;
            }
        }

        /// <summary>
        /// 获取指定类型文件
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="searchPattern"></param>
        public static string[] ListAllFilesWithFilter(string folderPath, string searchPattern)
        {
            try
            {
                // 获取文件夹内所有匹配过滤条件的文件路径
                string[] files = Directory.GetFiles(folderPath, searchPattern);

                return files;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
                return null;
            }
        }
        /// <summary>
        /// 获取指定类型文件
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="searchPattern"></param>
        public static string[] ListAllFilesWithFilter(string folderPath, string[] fileExtensions)
        {
            try
            {
                // 获取文件夹内所有匹配过滤条件的文件路径
                string[] files = Directory.GetFiles(folderPath)
                                          .Where(file => fileExtensions.Contains(Path.GetExtension(file).ToLower()))
                                          .ToArray();
                return files;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
                return null;
            }
        }

        /// <summary>
        /// 查找文件夹下面所有目录内的文件
        /// </summary>
        /// <param name="folderPath"></param>
        public static string[] ListAllFilesRecursive(string folderPath)
        {
            try
            {
                // 获取文件夹内所有文件的完整路径，包括子文件夹
                string[] files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
                return files;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
                return null;
            }
        }

        /// <summary>
        /// 获取文件夹下所有文件夹内指定文件类型的文件
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="fileExtensions"></param>
        public static string[] ListAllImageFiles(string folderPath, string[] fileExtensions, string searchPattern = "*")
        {
            try
            {
                // 获取文件夹内所有文件的完整路径，包括子文件夹
                string[] files = Directory.GetFiles(folderPath, $"{searchPattern}.*", SearchOption.AllDirectories)
                                          .Where(file => fileExtensions.Contains(Path.GetExtension(file).ToLower()))
                                          .ToArray();
                return files;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
                return null;
            }
        }
    }
}
